package com.itcast.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.mapper.CheckgroupMapper;
import com.itcast.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckgroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckgroupService{
    @Autowired
    private CheckgroupMapper checkgroupMapper;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkgroupMapper.findAll(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 设置checkitem和checkgroup的中间表关系
     * @param checkGroup
     * @param checkitemIds
     */
    public void setCheckitemandGroup(CheckGroup checkGroup,Integer[] checkitemIds){
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemid:checkitemIds){
                Map<String,Integer> map = new HashMap<>();
                map.put("checkitem_id",checkitemid);
                map.put("checkgroup_id",checkGroup.getId());
                checkgroupMapper.addcheckitemandgroup(map);
            }
        }
    }

    /**
     * 添加数据
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
            checkgroupMapper.add(checkGroup);
            setCheckitemandGroup(checkGroup,checkitemIds);
    }

    /**
     * 删除中间表关系
     * @param id
     */
    public void realtCheckitemdanGroup(Integer id){
        checkgroupMapper.realtCheckitemdanGroup(id);
    }

    /**
     * 删除数据
     * @param id
     */
    @Override
    public void delete(Integer id) {
        realtCheckitemdanGroup(id);
        checkgroupMapper.delete(id);
    }

    /**
     * 根据id查询数据实现数据的回显
     * @param id
     * @return
     */
    @Override
    public CheckGroup findByid(Integer id) {
        return checkgroupMapper.findByid(id);
    }

    /**
     * 根据checkgroup_id查找checkitem_id
     * @param id
     * @return
     */
    @Override
    public Integer[] findCheckitemIds(Integer id) {
        return checkgroupMapper.findCheckitemIds(id);
    }

    /**
     * 修改数据
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
        //删除checkitem和checkgroup的关系
        realtCheckitemdanGroup(checkGroup.getId());
        checkgroupMapper.update(checkGroup);
        setCheckitemandGroup(checkGroup,checkitemIds);
    }

    /**
     * 查询对象
     * @return
     */
    @Override
    public List<CheckGroup> findall() {
        return checkgroupMapper.findall();
    }
}
